<?php
require 'db.php';

if (isset ( $_GET ['dx']   )) {
	setcookie("TaiKhoan",'',time()-3600*24);
	setcookie("Passwork",'',time()-3600*24);
	unset($_COOKIE['TaiKhoan']);
	unset($_COOKIE['Passwork']);
	if (isset  ($_SESSION ['TaiKhoan'] ) && isset  ($_SESSION ['Passwork'] )) {
	unset($_SESSION['TaiKhoan']);
	unset($_SESSION['Passwork']);
	}
}

$login = false;
$type="";
if (isset ( $_COOKIE ['TaiKhoan'] ) && isset ( $_COOKIE ['Passwork'] )) {
	$UserDN = $_COOKIE ['TaiKhoan'];
	$matkhau = $_COOKIE ['Passwork'];
	$sql = "SELECT MaSV as tk, MATKHAU , 'SV' AS type from dbo_sinhvien where MaSV=:masv and MATKHAU=:matkhau union  SELECT MAGV as tk, MATKHAU , 'GV' AS type from dbo_giangvien  where MAGV=:magv and MATKHAU=:matkhaugv";
	$stmt = $db->prepare ( $sql );
	$stmt->bindParam ( ":masv", $UserDN );
	$stmt->bindParam ( ":matkhau", $matkhau );
	$stmt->bindParam ( ":magv", $UserDN );
	$stmt->bindParam ( ":matkhaugv", $matkhau );
	$stmt->execute ();
	while ($r= $stmt->fetch () ) {
		$login = true;
		$type=$r['type'];
	}
	if ($login) {
		
		setcookie ( "TaiKhoan", $UserDN, time () + 3600 * 24 );
		setcookie ( "Passwork", $matkhau, time () + 3600 * 24 );
	}
}
if (isset  ($_SESSION ['TaiKhoan'] ) && isset  ($_SESSION ['Passwork'] )) {
	$UserDN = $_SESSION ['TaiKhoan'];
	$matkhau = $_SESSION ['Passwork'];
	$sql = "SELECT MaSV as tk, MATKHAU , 'SV' AS type from dbo_sinhvien where MaSV=:masv and MATKHAU=:matkhau union  SELECT MAGV as tk, MATKHAU , 'GV' AS type from dbo_giangvien  where MAGV=:magv and MATKHAU=:matkhaugv";
	$stmt = $db->prepare ( $sql );
	$stmt->bindParam ( ":masv", $UserDN );
	$stmt->bindParam ( ":matkhau", $matkhau );
	$stmt->bindParam ( ":magv", $UserDN );
	$stmt->bindParam ( ":matkhaugv", $matkhau );
	$stmt->execute ();
	while ( $r=$stmt->fetch () ) {
		$login = true;
		$type=$r['type'];
	}
}
?>
	<?php
if (isset ( $_POST ['submit'] )) {
		
		$UserDN = $_POST ['TaiKhoan'];
		$matkhau = $_POST ['Passwork'];
		$sql = "SELECT MaSV as tk, MATKHAU , 'SV' AS type from dbo_sinhvien where MaSV=:masv and MATKHAU=:matkhau union  SELECT MAGV as tk, MATKHAU , 'GV' AS type from dbo_giangvien  where MAGV=:magv and MATKHAU=:matkhaugv";
		$stmt = $db->prepare ( $sql );
		$stmt->bindParam ( ":masv", $UserDN );
		$stmt->bindParam ( ":matkhau", $matkhau );
		$stmt->bindParam ( ":magv", $UserDN );
		$stmt->bindParam ( ":matkhaugv", $matkhau );
		$stmt->execute ();
		while ($r= $stmt->fetch () ) {
			$login = true;
			$type=$r['type'];
		}
		if ($login) {
			if (isset ( $_POST ['chkRemember'] )) {
				if ($_POST ["chkRemember"] == 'ok') {
					setcookie ( "TaiKhoan", $UserDN, time () + 3600 * 24 );
					setcookie ( "Passwork", $matkhau, time () + 3600 * 24 );
				}
			}else{
					$_SESSION ['TaiKhoan']=$UserDN;
					$_SESSION ['Passwork']=$matkhau;
				}
		} else {
			echo '<div class="alert alert-danger">Tài khoản mật khẩu không hợp lệ</div>';
		}
	}
	?>

	

